How to create user and grant permission in SQL server?
How to create user and grant permission in SQL server?
321
15-Jul-2024
Updated on 16-Jul-2024
Ashutosh Kumar Verma
16-Jul-2024SQL Server Create User and Grant Permission
Once a user is created using the
CREATE USERstatement, the user has no permissions on database objects such as tables, views, and indexes.Create New User
For creating new users in the database, first, we created a login
testLoginwith password,Note- use
masterdatabase for creating any login in the SQL database.Now, move to the current database to
MyCollegeDband create a user fortestLoginlogin.When we create a new user in the database he doesn't have any permissions on the database objects like tables, views, and indexes.
To allow a user to interact with database objects, you must grant permissions to the user. For example, you can grant permissions so that a user can select data from a table.
SQL Server GRANT statement
To grant permission to a user, you use the SQL
GRANTstatement.The
GRANTstatement allows you to grant permission on the securable to the principal.Syntax-
Explanation-
First, specify one or more permissions after the
GRANTkeyword. If you have multiple permissions, you must use a comma to separate the permissions.Second, specify a securable after the
ONkeyword.Third, specify a principal after the
TOkeyword.Example-
Let's create a table
Peoplein the databaseMyCollegeDband inserts some values into it,If you connect to SQL Server using login
testLogin. You will see that uservaranscan access theMyCollegeDbdatabase but can't see any tables. See in the below picture,Grant the SELECT Permission
Switch to the system administrator connection and grant
SELECTpermission to the uservaranson thePeopletable.Now, the user
varanscan see thePeopletable and select data from it. For example,However, the user
varanscannot insert or update data into thePeopletable.Multiple Grant Permission
Grant the
INSERTandDELETEpermissions on thePeopletable to the uservarans.Now, switch to the user
varansconnection and insert a new row into thePeopletable,Now, the user
varanscan insert data into and delete data from thePeopletable.Also, Read: How to drop a user in SQL Server?